import 'dart:async';

import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../string_res/str_res_keys.dart';

class BindSuccess extends StatefulWidget {
  final String title;
  final Function(bool backMain) bakc;
  const BindSuccess({super.key, required this.bakc, required this.title});

  @override
  State<BindSuccess> createState() => _BindSuccessState();
}

class _BindSuccessState extends State<BindSuccess> {
  Timer? timer;
  int timeNum = 3;

  @override
  void initState() {
    super.initState();
    startTimer();
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }

  void startTimer() {
    const oneSec = Duration(seconds: 1);
    timer = Timer.periodic(
      oneSec,
      (Timer timer) {
        if (timeNum == 0) {
          timer.cancel();
          Get.back();
          widget.bakc(true);
        } else {
          setState(() {
            timeNum--;
          });
        }
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
          color: Color(0xFF434650),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(60.sp),
            topRight: Radius.circular(60.sp),
          ),
        ),
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Row(mainAxisAlignment: MainAxisAlignment.end, children: [
            Image.asset('assets/images/home/main/close_icon.png',
                    width: 36.sp, height: 36.sp)
                .addGestureDetector(() => Get.back())
          ]).marginOnly(top: 54.sp, right: 30.sp, bottom: 54.sp),
          Container(height: 2.sp, color: Color(0xFFD8D8D8))
              .marginOnly(left: 32.sp, right: 32.sp, bottom: 32.sp),
          Image.asset('assets/images/home/scan/success_icon.png',
              width: 490.sp, height: 490.sp),
          DefaultText(widget.title, 48.sp,
                  textColor: Colors.white, fontWeight: FontWeight.w700)
              .marginOnly(top: 30.sp, bottom: 18.sp),
          DefaultText(SR.backMain.replaceAll('xxxx', '${timeNum}S'), 32.sp,
                  textColor: Colors.white)
              .marginOnly(bottom: 110.sp)
        ]));
  }
}
